package com.uenpay.im.ui.old.activity.movement;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.listview.RefreshListView;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * CopyRight:
 * <p>
 * Class Explain:活动列表
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */

public class MovementListActivity extends BaseActivity implements
        RefreshListView.IRefreshListViewListener {

    private int page = 1;
    private MovementHandler movementHandler = new MovementHandler();
    private RefreshListView listview;
    private MovementListAdapter adapter = null;
    private MovementBroadcastReceiver broadcastReceiver;
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_xlist_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        loading.setVisibility(View.VISIBLE);
        getData();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.movement));
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.movement_publish));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        listview = (RefreshListView) findViewById(R.id.discover_listview);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        listview.setPullLoadEnable(true);
        listview.setRefreshListViewListener(this);
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        ActivityHandle.startActivity(MovementListActivity.this,
                                PublishMovementActivity.class, null, -1);
                    }
                });
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                MovementEntity entity = (MovementEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (entity != null) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("movementId", entity.movementId);
                    ActivityHandle.startActivity(MovementListActivity.this,
                            MovementDetailActivity.class, bundle, -1);
                }
            }
        });
    }

    private void getData() {
        movementHandler.getMovementList(1, page, "",
                new MovementHandler.IGetMovementListResultListener() {

                    @Override
                    public void onResult(int status, String msg,
                                         ArrayList<MovementEntity> arrayList) {
                        onLoad();
                        loading.setVisibility(View.GONE);
                        if (status == HttpInvokeResultCode.RESULT_OK) {
                            if (page == 1) {
                                if (adapter == null) {
                                    adapter = new MovementListAdapter(
                                            MovementListActivity.this,
                                            arrayList);
                                } else {
                                    adapter.setData(arrayList);
                                }
                                listview.setPullLoadEnable(true);
                                listview.setAdapter(adapter);
                            } else {
                                if (arrayList != null && arrayList.size() > 0) {
                                    if (adapter == null) {
                                        adapter = new MovementListAdapter(
                                                MovementListActivity.this,
                                                arrayList);
                                        listview.setAdapter(adapter);
                                    } else {
                                        adapter.addData(arrayList);
                                    }
                                } else {
                                    listview.setPullLoadEnable(false);
                                }
                            }
                        }
                    }
                });
    }

    private void onLoad() {
        listview.stopRefresh();
        listview.stopLoadMore();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

    @Override
    public void onResume() {
        if (broadcastReceiver == null) {
            broadcastReceiver = new MovementBroadcastReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(BroadcastAction.ACTION_REFRESH_MOVEMENT);
            MovementListActivity.this.registerReceiver(broadcastReceiver,
                    filter);
        }
        super.onResume();
    }

    @Override
    public void onDestroy() {
        if (broadcastReceiver != null) {
            MovementListActivity.this.unregisterReceiver(broadcastReceiver);
        }
        super.onDestroy();
    }

    private class MovementBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (arg1.getAction()
                    .equals(BroadcastAction.ACTION_REFRESH_MOVEMENT)) {
                page = 1;
                getData();
            }
        }

    }

    ;
}
